package com.zch.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.action.CoreController;
import com.base.dao.model.Grid;
import com.base.dao.model.Ret;
import com.base.dialect.PaginationSupport;
import com.base.util.DateUtil;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.ActivitymessageExample;
import com.zch.dao.model.ZxMessage;
import com.zch.daoEx.model.ActivitymessageEx;
import com.zch.service.ActivitymessageService;

@RequestMapping("activitymessage")
@Controller
@LoginFilter
public class ActivitymessageController extends CoreController{

	@Autowired
	private ActivitymessageService activitymessageService;
	@Autowired
    private MessageService messageService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<ActivitymessageEx> list = activitymessageService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Activitymessage activitymessage){
		if (StringUtils.isBlank(activitymessage.getId())){
			activitymessageService.insert(activitymessage);
		}else{
			activitymessageService.updateByPrimaryKeySelective(activitymessage);
			activitymessage = activitymessageService.selectByPrimaryKey(activitymessage.getId());
	        String pushContent = "";
	        if (activitymessage.getIsAgree() == 2){
	            pushContent = "审核未通过！原因:"+activitymessage.getNotAgreeReason();
	            messageService.findMessage(activitymessage.getActivityName(), pushContent, activitymessage.getUserId(), 1);
	        }
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Activitymessage activitymessage = activitymessageService.selectByPrimaryKey(id);
		activitymessage.setContent(activitymessage.getContent().replace("\r\n", "<br/>"));
		return JSONUtils.serialize(activitymessage);
	}
	
	@RequestMapping("/findByIds")
    @ResponseBody
    public String finds(String id){
        Activitymessage activitymessage = activitymessageService.selectByPrimaryKey(id);
        activitymessage.setContent(activitymessage.getContent().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        return JSONUtils.serialize(activitymessage);
    }

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			activitymessageService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

    @RequestMapping("/update")
    @ResponseBody
    public String update(Activitymessage activitymessage){
        int result = activitymessageService.updateByPrimaryKeySelective(activitymessage);
        activitymessage = activitymessageService.selectByPrimaryKey(activitymessage.getId());
        String pushContent = "";
        if(activitymessage.getIsAgree() == 1){
             pushContent = "审核通过！";
             messageService.findMessage(activitymessage.getActivityName(), pushContent, activitymessage.getUserId(), 1);
        }       
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/myList")
    @ResponseBody
    public String myList(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("id", id);
        List<ActivitymessageEx> list = activitymessageService.selectMyList(map);
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }
    
    @RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(Activitymessage activitymessage){
        activitymessage.setUserId(getSessionParameter("id"));
        if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
        if (StringUtils.isBlank(activitymessage.getId())){
            activitymessageService.insert(activitymessage);
        }else{
            activitymessageService.updateByPrimaryKeySelective(activitymessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/top")
    @ResponseBody
    public String top(Activitymessage activitymessage){
        if (StringUtils.isBlank(activitymessage.getId())){
            activitymessage.setPublishTime(new Date());
            activitymessageService.insert(activitymessage);
        }else{
            activitymessage.setPublishTime(new Date());
            activitymessageService.updateByPrimaryKeySelective(activitymessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/cancelTops")
    @ResponseBody
    public String cancelTops(Activitymessage activitymessage){
        if (StringUtils.isBlank(activitymessage.getId())){
            activitymessage.setPublishTime(null);
            activitymessageService.insert(activitymessage);
        }else{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", activitymessage.getId());
            int result = activitymessageService.cancleTops(map);
        }
        return JSONUtils.serialize(new Ret(0));
    }
}